﻿using Microsoft.AspNetCore.Mvc;
using 天离安全_天擎.Tl_Class_Services;
using 天离安全_天擎.Tl_SerVices;

namespace 天离安全_天擎.Controllers.Crawler_Controller
{
    /// <summary>
    /// 爬虫模块
    /// </summary>
    [ApiController]
    [Route("[controller]")]
    public class CrawlerScanController : Controller
    {
        /// <summary>
        /// 默认爬虫-基础表面层获取所有资源文件，采用监听模式对所有加载资源进行获取，代理请求如果不可访问将自动跳过代理，服务器需要放行8000-9000内的端口
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost("Scan")]
        public async Task<IActionResult> CrawlerScan([FromBody] CrawlerScanDefault request)
        {
            // 传出代理状态
            Tl_Api.CrawlerScan_Api.Proxy_Status = request.Proxy_Status;
            // 传出代理地址
            Tl_Api.CrawlerScan_Api.Socks5Proxy_IP = request.Socks5Proxy_Ip;
            // 传出代理端口
            Tl_Api.CrawlerScan_Api.Socks5Proxy_Port = request.Socks5Proxy_Port;

            var analyzer = new Tl_SerVices_WebPageAnalyzer();
            await analyzer.FetchAssetsFromUrlAsync(request.Url);

            var suesscc = Tl_Api.CrawlerScan_Api.Scan_Info;
            var error = Tl_Api.CrawlerScan_Api.Scan_Error;

            var intResultss = new List<string>();
            var intResults = new List<string>();

            if (suesscc.Length == 0 && error.Length == 0)
            {
                return Ok(new { status = "504" });
            }

            if (suesscc.Length > 0)
            {
                var crawler = suesscc.ToString().Split(new[] { "\r\n", "\n" }, StringSplitOptions.RemoveEmptyEntries);

                foreach (var success in crawler)
                {
                    intResults.Add(success);
                }
                return Ok(new { status = "200", info = intResults.Distinct(), datetime = DateTime.Now.ToString() });
            }
            else
            {
                foreach (var errors in error.ToString())
                {
                    intResultss.Add(errors.ToString());
                }
            }

            return Ok(new { status = "500", error = intResultss, datetime = DateTime.Now.ToString() });
        }
        /// <summary>
        /// 检查代理可用性
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost("CheckProxy")]
        public async Task<IActionResult> CheckProxy([FromBody] CheckProxy request)
        {
            // 传出代理地址
            Tl_Api.CrawlerScan_Api.Socks5Proxy_IP = request.Socks5Proxy_Ip;
            // 传出代理端口
            Tl_Api.CrawlerScan_Api.Socks5Proxy_Port = request.Socks5Proxy_Port;

            var analyzer = new CheckProxy_Services();
            await analyzer.CheckProxyAsync();

            var suesscc = Tl_Api.CrawlerScan_Api.Scan_Info;
            var error = Tl_Api.CrawlerScan_Api.Scan_Error;

            var intResultss = new List<string>();
            var intResults = new List<string>();

            if (suesscc.Length == 0 && error.Length == 0)
            {
                return Ok(new { status = "504" });
            }

            if (suesscc.Length > 0)
            {
                var crawler = suesscc.ToString().Split(new[] { "\r\n", "\n" }, StringSplitOptions.RemoveEmptyEntries);

                foreach (var success in crawler)
                {
                    intResults.Add(success);
                }
                return Ok(new { status = "200", info = intResults.Distinct(), datetime = DateTime.Now.ToString() });
            }
            else
            {
                var crawler = error.ToString().Split(new[] { "\r\n", "\n" }, StringSplitOptions.RemoveEmptyEntries);

                foreach (var errors in crawler)
                {
                    intResultss.Add(errors.ToString());
                }
            }

            return Ok(new { status = "500", error = intResultss, datetime = DateTime.Now.ToString() });
        }
    }
}
